package com.exchange.service;

import com.exchange.dto.MerchantDto;
import com.exchange.entity.Merchant;
import com.exchange.repository.MerchantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Optional;

@Service
public class MerchantService {
    
    @Autowired
    private MerchantRepository merchantRepository;
    
    public Optional<MerchantDto> findByUserId(Long userId) {
        return merchantRepository.findByUserId(userId)
                .map(MerchantDto::fromEntity);
    }
    
    public Optional<MerchantDto> findById(Long id) {
        return merchantRepository.findById(id)
                .map(MerchantDto::fromEntity);
    }
    
    public MerchantDto createMerchant(Merchant merchant) {
        merchant.setCreatedAt(java.time.LocalDateTime.now());
        merchant.setUpdatedAt(java.time.LocalDateTime.now());
        Merchant savedMerchant = merchantRepository.save(merchant);
        return MerchantDto.fromEntity(savedMerchant);
    }
    
    public MerchantDto updateMerchant(Long id, Merchant merchantDetails) {
        Optional<Merchant> existingMerchant = merchantRepository.findById(id);
        if (existingMerchant.isPresent()) {
            Merchant merchant = existingMerchant.get();
            merchant.setBusinessName(merchantDetails.getBusinessName());
            merchant.setBusinessLicense(merchantDetails.getBusinessLicense());
            merchant.setContactPerson(merchantDetails.getContactPerson());
            merchant.setAddress(merchantDetails.getAddress());
            merchant.setSubscriptionType(merchantDetails.getSubscriptionType());
            merchant.setSubscriptionStartDate(merchantDetails.getSubscriptionStartDate());
            merchant.setSubscriptionEndDate(merchantDetails.getSubscriptionEndDate());
            merchant.setMaxProducts(merchantDetails.getMaxProducts());
            merchant.setMaxExchangeCodes(merchantDetails.getMaxExchangeCodes());
            merchant.setMaxOrdersPerMonth(merchantDetails.getMaxOrdersPerMonth());
            merchant.setUpdatedAt(java.time.LocalDateTime.now());
            
            Merchant updatedMerchant = merchantRepository.save(merchant);
            return MerchantDto.fromEntity(updatedMerchant);
        }
        return null;
    }
    
    public boolean isSubscriptionActive(Long merchantId) {
        Optional<Merchant> merchant = merchantRepository.findById(merchantId);
        return merchant.map(Merchant::isSubscriptionActive).orElse(false);
    }
    
    public boolean hasReachedResourceLimit(Long merchantId, String resourceType) {
        Optional<Merchant> merchantOpt = merchantRepository.findById(merchantId);
        if (!merchantOpt.isPresent()) {
            return true; // If merchant doesn't exist, limit is reached
        }
        
        Merchant merchant = merchantOpt.get();
        
        switch (resourceType) {
            case "products":
                return merchant.getSubscriptionType() == Merchant.SubscriptionType.FREE && 
                       merchant.getMaxProducts() <= 0;
            case "exchange_codes":
                return merchant.getSubscriptionType() == Merchant.SubscriptionType.FREE && 
                       merchant.getMaxExchangeCodes() <= 0;
            case "orders":
                return merchant.getSubscriptionType() == Merchant.SubscriptionType.FREE && 
                       merchant.getMaxOrdersPerMonth() <= 0;
            default:
                return false;
        }
    }
}